
******PARTNERSHIP (TABLE E.1, TABLE E.2, FIGURE E.1)********

set more off
clear all 
clear matrix
capture log close

adopath + "$main/DoFiles"																		


local seed = 999
local reps = 5000




//--> DATA: FA CO PANEL TO IDENTIFY PRE BONUS RATE COMANAGMENT IN FA'S CO PORFOLIO

		use "$data/FWCO_panel.dta", clear
		
		//--> KEEP PRE BONUS MONTHS
		
				keep if month<10
				
		//--> CO-MANAGED CO INDICATOR
	
			bys co_code month: gen temp_coshare = _N
			g num_co_share = 0
			replace num_co_share = 1 if temp_coshare>1
			gen num_co = 1
			
			
			//--> SOME ARE CG (CREDIT GROUP)--> WEIGHT THEN .25 
			
					preserve
					use "$data/MIS_comthlevel.dta", clear
					bys co_code: keep if _n==1
					keep co_code credit_gp
					tempfile temp_cg
					save `temp_cg'
					restore
					
					sort co_code 
					merge co_code using `temp_cg', nok
					drop _m
					
					
					replace num_co_share = 0.25 if temp_coshare>1 & credit_gp==1
					replace num_co = 0.25 if temp_coshare>1 & credit_gp==1
					
			//--> FA LEVEL SHARE OF CO COMANAGED
			
					collapse (sum) num_co num_co_share, by(fw_code)
					gen share_coshare = num_co_share/num_co 
					
					
			//--> DEFINE PARTNERSHIP DUMMY VAR
			
					_pctile share_coshare, n(4)
					g partner = (share_coshare>r(r2))
					
					keep fw_code partner share_coshare
					
					
			//--> TREATMENT STATUS AND BASELINE VARS
			
					sort fw_code 
					merge fw_code using "$data/FWHR.dta"
					drop _m
					
					preserve
						use "$data/baseline_fa_vars.dta", clear
						local credit_var weighted_num_active_loan weighted_num_new_loan weighted_amount_issue wmiss_pay_maturity_month inv_ratio_miss_pay_month inv_ratio_miss_pay_20 stdcredit_index
						foreach v in `credit_var' {
							rename `v' `v'0
							}
						sort fw_code
						tempfile temp
					save `temp'
					restore
				
					sort fw_code
					merge fw_code using `temp'
					drop _m
								

					
			//--> CREDIT AND SOCIAL INDEX 
			
					sort fw_code
					merge fw_code using "$data/social_fa_vars.dta"
					drop _m
					
					sort fw_code
					merge fw_code using "$data/credit_fa_vars.dta"
					drop _m
					
					
			//--> INTERACTIONS VARS
			
					g treat_credit_partner = treat_credit*partner
					g treat_sm_partner = treat_sm*partner
			
			//--> REGION DUMMIES		
					
					tab region, gen(r)	
					
			
					
//--> OUTPUT: ONLINE APPENDIX TABLE E.2 (DIFFERENTIAL EFFECTS BY BASELINE PARTNERSHIP)
	*********************************************************************************************************************************************************

	
		//--> REGRESSION OUTPUT (FOR ESTIMATED COEEFICIENTS ONLY)
			****************************************		

 
				reg stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 treat_credit treat_sm treat_credit_partner treat_sm_partner partner
				sum `v' if treat==0
				local m = r(mean)
				outreg2 using "$output/tableE_2 (coeff).xls", replace addstat("CONTROL MEAN", `m') noaster stats(coef) keep(treat_credit treat_sm treat_credit_partner treat_sm_partner partner) nocons nor2
				
				reg stdsocial_index r1 r2 r3 treat_credit treat_sm treat_credit_partner treat_sm_partner partner
				sum `v' if treat==0
				local m = r(mean)
				outreg2 using "$output/tableE_2 (coeff).xls", addstat("CONTROL MEAN", `m') noaster stats(coef) keep(treat_credit treat_sm treat_credit_partner treat_sm_partner partner) nocons nor2
									
									
		log using "$output/tableE_2 (p-values)", replace	
		
		//--> CALCULATE WILDCLUSTER T-BOOTSTRAP P VALUES
			****************************************		
			
			//--> P-VALUES FOR TC, TS, TCXP, TSXP, and P
				
					cgmwildboot stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 treat_credit treat_sm treat_credit_partner treat_sm_partner partner, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed') 
					cgmwildboot stdsocial_index r1 r2 r3 treat_credit treat_sm treat_credit_partner treat_sm_partner partner, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed') 
					
							
			//--> F-TEST: TC + PXTC = 0
			
					g test2 = treat_credit
					g x2 = treat_credit_partner - treat_credit

					cgmwildboot stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 test2 treat_sm partner x2 treat_sm_partner, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
					cgmwildboot stdsocial_index r1 r2 r3 test2 treat_sm partner x2 treat_sm_partner, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
	
			//--> F-TEST: TS + PXTS = 0
			
					g test3 = treat_sm
					g x3 = treat_sm_partner - treat_sm
							
					cgmwildboot stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 treat_credit test3 partner treat_credit_partner x3, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
					cgmwildboot stdsocial_index r1 r2 r3 treat_credit test3 partner treat_credit_partner x3, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
					
			//--> F-TEST: TS = TC
			
					g test4 = treat_credit
					g x4 = treat_credit + treat_sm
							
					cgmwildboot stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 test4 x4 partner treat_credit_partner treat_sm_partner, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
					cgmwildboot stdsocial_index r1 r2 r3 test4 x4 partner treat_credit_partner treat_sm_partner, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
					
			
			//--> F-TEST: TSxP = TCxP
					
					g test5 = treat_credit
					g x5_1 = treat_credit + treat_sm
					g x5_2 = treat_credit_partner - treat_credit
					g x5_3 = treat_credit + treat_sm_partner
					
					cgmwildboot stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 test5 partner x5_1 x5_2 x5_3, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
					cgmwildboot stdsocial_index r1 r2 r3 test5 partner x5_1 x5_2 x5_3, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')

	

		//--> CALCULATE RANDOMIZATION INFERENCE BASED P VALUES
			****************************************						
						
			//--> P-VALUES FOR TC, TS, TCXP, TSXP, and P				
					
					randcmd((treat_credit treat_sm treat_credit_partner treat_sm_partner partner) reg stdcredit_index stdcredit_index0 mstdcredit_index treat_credit treat_credit_partner treat_sm_partner treat_sm partner r1 r2 r3, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace treat_credit_partner = treat_credit*partner) calc2(replace treat_sm_partner = treat_sm*partner)		
					randcmd((treat_credit treat_sm treat_credit_partner treat_sm_partner partner) reg stdsocial_index treat_credit treat_credit_partner treat_sm_partner treat_sm partner r1 r2 r3, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace treat_credit_partner = treat_credit*partner) calc2(replace treat_sm_partner = treat_sm*partner)		

			//--> F-TEST: TC + PXTC = 0
			
					randcmd((test2) reg stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 test2 treat_sm partner x2 treat_sm_partner, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace test2 = treat_credit) calc2(replace treat_sm_partner = treat_sm*partner)		
					randcmd((test2) reg stdsocial_index r1 r2 r3 test2 treat_sm partner x2 treat_sm_partner, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace test2 = treat_credit) calc2(replace treat_sm_partner = treat_sm*partner)		

			//--> F-TEST: TS + PXTS = 0
				
					randcmd((test3) reg stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 treat_credit test3 partner treat_credit_partner x3, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace test3 = treat_sm) calc2(replace treat_sm_partner = partner*treat_sm) calc3(replace x3 = treat_sm_partner - treat_sm)		
					randcmd((test3) reg stdsocial_index r1 r2 r3 treat_credit test3 partner treat_credit_partner x3, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace test3 = treat_sm) calc2(replace treat_sm_partner = partner*treat_sm) calc3(replace x3 = treat_sm_partner - treat_sm)		

				
			//--> F-TEST: TS = TC
			
					randcmd((test4) reg stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 test4 x4 partner treat_credit_partner treat_sm_partner, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace test4 = treat_credit) calc2(replace x4 = treat_credit + treat_sm) 	
					randcmd((test4) reg stdsocial_index r1 r2 r3 test4 x4 partner treat_credit_partner treat_sm_partner, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace test4 = treat_credit) calc2(replace x4 = treat_credit + treat_sm) 	

					
			//--> F-TEST: TSxP = TCxP

					randcmd((test5) reg stdcredit_index stdcredit_index0 mstdcredit_index r1 r2 r3 test5 partner x5_1 x5_2 x5_3, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace test5 = treat_credit) calc2(replace treat_credit_partner = treat_credit*partner) calc3(replace treat_sm_partner = treat_sm*partner) calc4(replace x5_1 = treat_credit + treat_sm)	calc5(replace x5_2 = treat_credit_partner - treat_credit) calc6(replace x5_3 = treat_credit + treat_sm_partner)
					randcmd((test5) reg stdsocial_index r1 r2 r3 test5 partner x5_1 x5_2 x5_3, cluster(fu_feb05)), treatvars(treat_credit treat_sm) groupvar(fu_feb05)reps(`reps') seed(`seed') calc1(replace test5 = treat_credit) calc2(replace treat_credit_partner = treat_credit*partner) calc3(replace treat_sm_partner = treat_sm*partner) calc4(replace x5_1 = treat_credit + treat_sm)	calc5(replace x5_2 = treat_credit_partner - treat_credit) calc6(replace x5_3 = treat_credit + treat_sm_partner)

					
		log close
	*********************************************************************************************************************************************************
										
										
//--> OUTPUT: ONLINE APPENDIX TABLE E.1 (BASELINE DIFF BET PARTNER AND NONPARTNER FAS)
	*********************************************************************************************************************************************************
					
					
		g bonus_status = 0 if treat==0
		replace bonus_status = 1 if treat_credit ==1
		replace bonus_status = 2 if treat_sm ==1
		
		
		//--> CONVERT NOT FIRST JOB TO FIRST JOB
		
			replace nfirst_job = 1 - nfirst_job
						
						
		local demo_var age female marital_status head_hh education consumption_hh
		local emp_var work_month nfirst_job work_vb weighted_num_co_alive
		local pref_var still_work_nrsp_2years overtime_perday credit_bonus soc_help_credit volunteer_before_nrsp job_like_help 

		local var_longlist `demo_var' `emp_var' `pref_var'
		
		
		//--> COLUMNS 1-3: MEANS BY TREATMENT STATUS
			*****************************************
		
				putexcel set "$output/tableE_1 (col1-2)", replace
					putexcel A1 = "var name"
					putexcel B1 = "non-partnered"
					putexcel C1 = "partnered"
				
				local count = 2
				foreach v in `var_longlist' {
					mean `v', over (partner)
						putexcel A`count' = "`v'"
						putexcel B`count' = matrix(e(b))
					local count = `count' + 1
					}
					putexcel A`count' = "No. of FAs"
					putexcel B`count' = matrix(e(_N))
					
					local count = `count' + 1
					putexcel A`count' = "Share of COs co-managed"
					mean share_coshare, over (partner)
					putexcel B`count' = matrix(e(b))

					
		//--> CALCULATE WILDCLUSTER T-BOOTSTRAP P VALUES
				****************************************					
					
				log using "$output/tableE_1 (p-values)", replace	
			
					foreach x in `var_longlist' share_coshare {
						cgmwildboot `x' partner, cluster(fu_feb05) bootcluster(fu_feb05) seed(`seed')
						}
		
//--> OUTPUT: ONLINE APPENDIX FIGURE E.1
	*********************************************************************************************************************************************************
			_pctile share_coshare, n(4)
			
			histogram share_coshare, width(0.1) start (0) frac xline(`r(r2)') lpattern(dash)
			graph save Graph "$output/graphE1.gph", replace
